﻿namespace com.stifel.BranchMgmtDao
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Microsoft.SharePoint.Linq;

    using Context = BranchMgmtDataContext;

    using CustomObject = StifelBranchPromotionWorklist;

    public class StifelBranchPromotionWorklistDao : StifelBaseDao
    {
        #region Fields

        public const string ListName = "stifelbranchpromotionworklist";

        #endregion Fields

        #region Constructors

        public StifelBranchPromotionWorklistDao( )
            : base(ListName)
        {
            CurrentWebUrl = ConfiguredWebUrl;
        }

        public StifelBranchPromotionWorklistDao( string currentWebUrl )
            : base(ListName)
        {
            this.CurrentWebUrl = currentWebUrl;
        }

        #endregion Constructors

        #region Methods

        public void Add( List<CustomObject> value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                l.InsertAllOnSubmit( value );
                c.SubmitChanges( );
            }
        }

        public CustomObject Add( CustomObject value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                string title = value.Title;
                Guid g = Guid.NewGuid( );
                value.Title = g.ToString( );
                l.InsertOnSubmit( value );
                c.SubmitChanges( );
                CustomObject v = ( from o in l
                                   where o.Title == g.ToString( )
                                   select o ).SingleOrDefault( );
                v.Title = title;
                c.SubmitChanges( );
                return v;
            }
        }

        public void Clear( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                var v = ( from o in l
                          where o.Id > 0
                          select o );
                if ( v.Count( ) > 0 )
                {
                    l.DeleteAllOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public void Delete( Guid uniqueId )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.UniqueId == uniqueId
                                   select o ).SingleOrDefault( );
                if ( v != null )
                {
                    l.DeleteOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public CustomObject Get( int id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.Id == id
                                   select o ).SingleOrDefault( );
                return v;
            }
        }

        public CustomObject Get( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.UniqueId == id
                                   select o ).SingleOrDefault( );
                return v;
            }
        }

        public List<CustomObject> GetAll( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                List<CustomObject> l = c.GetList<CustomObject>( ListTitle ).ToList( );
                return l;
            }
        }

        public List<CustomObject> GetByBranchLocation( int id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                var v = ( from o in l
                          where o.BranchLookupId == id
                          select o );
                if ( v == null )
                    return new List<CustomObject>( );
                else
                    return v.ToList( );
            }
        }

        public CustomObject GetByTitle( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.Title == id.ToString( )
                                   select o ).SingleOrDefault( );
                return v;
            }
        }

        public CustomObject Update( CustomObject value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.UniqueId == value.UniqueId
                                   select o ).SingleOrDefault( );
                if ( v != null )
                {
                    v.BranchLookupId = value.BranchLookupId;
                    v.PromotionDetails = value.PromotionDetails;
                    v.Title = value.Title;
                    // commit changes
                    c.SubmitChanges( );
                    return v;
                }
                else
                    return null;
            }
        }

        #endregion Methods
    }
}